﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Media;
using System.Xml.Serialization;

namespace WPLottery.Model
{
    public class NumberRow : INotifyPropertyChanged
    {
        public int Id { get; set; }

        [DataMember(Name = "Channel")]
        public string Channel { get; set; }

        public NumberRow()
        {
        }

        public NumberRow(int n0, int n1, int n2, int n3, int n4, int n5)
        {
            Number0 = n0;
            Number1 = n1;
            Number2 = n2;
            Number3 = n3;
            Number4 = n4;
            Number5 = n5;
        }

        private int m_n0;

        [DataMember(Name = "Number0")]
        public int Number0 {
            get { return m_n0; }
            set
            {
                if (value != m_n0)
                {
                    m_n0 = value;
                    OnPropertyChanged("Number0");
                }
            }
        }

        private int m_n1;

        [DataMember(Name = "Number1")]
        public int Number1
        {
            get { return m_n1; }
            set
            {
                if (value != m_n1)
                {
                    m_n1 = value;
                    OnPropertyChanged("Number1");
                }
            }
        }

        private int m_n2;

        [DataMember(Name = "Number2")]
        public int Number2
        {
            get { return m_n2; }
            set
            {
                if (value != m_n2)
                {
                    m_n2 = value;
                    OnPropertyChanged("Number2");
                }
            }
        }

        private int m_n3;

        [DataMember(Name = "Number3")]
        public int Number3
        {
            get { return m_n3; }
            set
            {
                if (value != m_n3)
                {
                    m_n3 = value;
                    OnPropertyChanged("Number3");
                }
            }
        }

        private int m_n4;

        [DataMember(Name = "Number4")]
        public int Number4
        {
            get { return m_n4; }
            set
            {
                if (value != m_n4)
                {
                    m_n4 = value;
                    OnPropertyChanged("Number4");
                }
            }
        }

        private int m_n5;

        [DataMember(Name = "Number5")]
        public int Number5
        {
            get { return m_n5; }
            set
            {
                if (value != m_n5)
                {
                    m_n5 = value;
                    OnPropertyChanged("Number5");
                }
            }
        }

        private Brush m_rowBrush = new SolidColorBrush(Colors.Transparent);

        [XmlIgnore]
        public Brush RowBrush
        {
            get { return m_rowBrush; }
            private set
            {
                if (value != m_rowBrush)
                {
                    m_rowBrush = value;
                    OnPropertyChanged("RowBrush");
                }
            }
        }

        private Brush m_rowColor = new SolidColorBrush(Colors.White);

        [XmlIgnore]
        public Brush RowColor
        {
            get { return m_rowColor; }
            private set
            {
                if (value != m_rowColor)
                {
                    m_rowColor = value;
                    OnPropertyChanged("RowColor");
                }
            }
        }

        public bool Current
        {
            get { return m_rowState == 0; }
            set
            {
                if (Current != value)
                {
                    RowState = value ? 0 : -1;
                    RowColor = value ? new SolidColorBrush(Colors.White) : new SolidColorBrush(Colors.Gray);
                    OnPropertyChanged("Current");
                }
            }
        }

        // -1 = disabled; 0 = loser; 1 - 9 = various levels of winning
        private int m_rowState = 0;

        [XmlIgnore]
        public int RowState
        {
            get { return m_rowState; }
            private set
            {
                if (value != m_rowState)
                {
                    m_rowState = value;
                    OnPropertyChanged("RowState");
                }
            }
        }

        private void OnPropertyChanged(string name)
        {
            PropertyChangedEventHandler temp = PropertyChanged;

            if (null != temp)
            {
                temp(this, new PropertyChangedEventArgs(name));
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;

        public bool IsWinner(int n0, int n1, int n2, int n3, int n4, int n5)
        {
            // Now implemented on the server
            bool pbMatch = n5 == Number5;

            int retVal = (new List<int>() { Number0, Number1, Number2, Number3, Number4 })
                        .Intersect(new List<int>() { n0, n1, n2, n3, n4 }).ToList().Count();

            if (retVal < 3)
            {
                if (!pbMatch)
                {
                    RowState = -1;
                }
                else
                {
                    RowState = retVal + 1;
                }
            }
            else
            {
                RowState 
                    = retVal == 3
                    ? 3
                    : retVal == 4
                    ? 6
                    : 8;

                if (pbMatch)
                    RowState++;
            }

            RowBrush = RowState > 0? new SolidColorBrush(Colors.Red) : new SolidColorBrush(Colors.Transparent);

            return RowState > 0;
        }
    }
}
